<?php
namespace app\index\controller;

use think\Controller;
use think\Image;
use oss\Oss as aliyunoss;
use think\Exception;
use app\app\job\Index;
// 指定允许其他域名访问
header('Access-Control-Allow-Origin:*');
// 响应类型
header('Access-Control-Allow-Methods:POST'); 
// 响应头设置
header('Access-Control-Allow-Headers:x-requested-with,content-type');

class Base extends Controller
{
    public $username;
    public $manage_type;
    public $banzhuren_type;
    public $zu_id;
    public $pid;
    public $gonghai;
    public $is_xiaoshou;
    public $group_name;
    public $manage;
    public function _initialize()
    {
        $controllerName = request()->controller();
        $actionName = request()->action();
        if ($controllerName == 'Index' && $actionName == 'uploadonefile') {
        }else{
            $token = input('token');
            $this->manage = $manage = self::checkToken($token);
            $this->username=$manage['username'];
            $this->manage_type=$manage['manage_type'];
            $this->banzhuren_type=$manage['banzhuren_type'];
            $this->zu_id=$manage['zu_id'];
            $this->pid=$manage['pid'];
            $this->gonghai=$manage['gonghai'];
            $this->is_xiaoshou=$manage['is_xiaoshou'];
            $this->group_name=$manage['group_name'];
        }
    }


    // 验证token是否有效
    public function checkToken($token)
    {
        if (empty($token)) {
            apiReturn(10001,'token不存在');
        }
        // $oneManage = db('crm_manage')->where('token',$token)->find();
        $oneManage = db('crm_manage')->alias('a')->join('tp_crm_group b','a.group_id=b.id')->field('a.name,a.username,a.zu_id,a.is_xiaoshou,a.type as manage_type,a.banzhuren_type,b.name as group_name,zu_watch,a.group_id')->where(array('a.token'=>$token))->find();
        if (empty($oneManage)) {
            apiReturn(10001,'登录失效');
        }
        $fenzuInfo = db('crm_fenzu')->field('pid,gonghai')->where(array('id'=>$oneManage['zu_id']))->find();
        $oneManage['pid'] = $fenzuInfo['pid'];
        $oneManage['gonghai'] = $fenzuInfo['gonghai'];
        return $oneManage;
    }

    /**
     * 添加操作记录
     * @param  $username              操作人
     * @param  $type                  操作类型
     * @param  $information_id        资料id
     * @param  $content               资料名称
     * @return bool
     */
    public function operationAdd($username, $type, $information_id, $content)
    {
        $manage = db('crm_manage')->field('username,name')->where('username',$username)->find();
        if (empty($manage)) {
            apiReturn(201,'操作员不存在');
        }
        $datas = array(
            'manage_id'=>$manage['username'],
            'manage_name'=>$manage['name'],
            'type'=>$type,
            'information_id'=>$information_id,
            'content'=>$content,
            'date'=>time()
        );
        db('crm_manage_operation')->insert($datas);
        return true;
    }

    /**
     * 添加跟进记录
     * @param $client_id             客户id集合(1,2,3)
     * @param $arrange_manage_id     安排人(管理员工号)
     * @param $follow_type           跟进标签
     * @param $content               跟进内容
     * @param $wait_deal             是否设置待办
     * @param $follow_manage_id      跟进人id 
     * @param $wait_deal_time        待办时间
     * @param $is_move               是否是转给
     * @return bool
     */
    public function followAdd($client_id, $arrange_manage_id, $follow_type, $content, $wait_deal = 0, $follow_manage_id = '', $wait_deal_time = '', $is_move = 0, $customer_label = 0, $jiaowu_manage_id = null, $banzhuren_manage_id = null, $order_id = null)
    {
        if (empty($arrange_manage_id)) {
            apiReturn(201,'安排人id不能为空');
        }
        if (empty($content)) {
            apiReturn(202,'跟进内容不能为空');
        }
        $client_ids = explode(',', $client_id);
        foreach ($client_ids as $key => $value) {
            if (!empty($customer_label)) {
                db('crm_customer')->where('client_id',$value)->update(['customer_label'=>$customer_label]);
            }
            $customer_name = db('crm_customer')->where('client_id',$value)->value('name');
            $arrange_manage_name = db('crm_manage')->where('username',$arrange_manage_id)->value('name');
            $follow_datas = array(
                'client_id'=>$value,
                'customer_name'=>$customer_name,
                'arrange_manage_id'=>$arrange_manage_id,
                'arrange_manage_name'=>$arrange_manage_name,
                'follow_type'=>$follow_type,
                'content'=>$content,
                'date'=>time()
            );
            if ($jiaowu_manage_id) {
                $follow_datas['jiaowu_manage_id'] = $jiaowu_manage_id;
            }
            if ($banzhuren_manage_id) {
                $follow_datas['banzhuren_manage_id'] = $banzhuren_manage_id;
            }
            if ($order_id) {
                $follow_datas['order_id'] = $order_id;
            }
            if ($wait_deal == 1) {
                if ($is_move == 1) {
                    $follow_datas['wait_deal'] = 0;
                }else{
                    $follow_datas['wait_deal'] = 1;
                }
                $follow_datas['follow_manage_id'] = $follow_manage_id;
                $follow_manage_name = db('crm_manage')->where('username',$follow_manage_id)->value('name');
                $follow_datas['follow_manage_name'] = $follow_manage_name;
                $follow_datas['genjin_date'] = strtotime($wait_deal_time);
            }
            db('crm_customer_follow')->insert($follow_datas); 
        }
        return true;
    }

    /**
     * 添加客户课程
     * @param $customer_id     客户id
     * @param $course_id       课程id
     * @return bool
     */
    public function couserAdd($customer_id, $course_id)
    {
        if (empty($customer_id)) {
            apiReturn(201,'客户id不能为空');
        }
        if (empty($course_id)) {
            apiReturn(202,'课程id不能为空');
        }
        $exist = db('crm_customer_course')->where(array('customer_id'=>$customer_id,'course_id'=>$course_id))->value('id');
        if (empty($exist)) {
            $datas = array(
                'customer_id'=>$customer_id,
                'course_id'=>$course_id,
                'status'=>1,
                'date'=>time()
            );
            db('crm_customer_course')->insert($datas);
        }
        return true;
    }

    /**
     * 添加客户课程白名单
     * @param $client_id     客户id
     * @param $course_id     课程id
     * @return bool
     */
    public function baimingdanAdd($client_id, $course_id)
    {
        if (empty($client_id)) {
            apiReturn(201,'客户id不能为空');
        }
        if (empty($course_id)) {
            apiReturn(202,'课程id不能为空');
        }
        $phone = db('crm_customer')->where('client_id',$client_id)->value('phone');
        $exist = db('goods_phone')->where(array('phone'=>$phone,'course_id'=>$course_id))->value('id');
        if (empty($exist)) {
            //开启白名单
            $phone_datas = array(
                'course_id'=>$course_id,
                'phone'=>$phone,
                'date'=>time()
            );
            db('goods_phone')->insert($phone_datas);
            self::clearCustomerCache($phone);
            //更新客户公开课信息
            $goods = db('goods')->where('id',$course_id)->find();

            if ($goods['search_type'] == 1 || $goods['search_type'] == 2 || $goods['search_type'] == 3) {
                if (($goods['id'] != 78) && ($goods['id'] != 67)) {
                    $next_course = db('goods_next')->field('id')->where('goods_id',$course_id)->limit(0,2)->order('id ASC')->select();
                    if (count($next_course) == 2) {
                        if (isset($next_course[0]['id']) && $next_course[0]['id'] != null) {
                            $customer_datas['public_yingqi'] = $course_id;
                            $customer_datas['public_yingqi_course1'] = $next_course[0]['id'];
                            $customer_datas['public_yingqi_course2'] = $next_course[1]['id'];
                            db('crm_customer')->where('client_id',$client_id)->update($customer_datas);

                            $yingqi_count_exist = db('crm_yingqi_count')->where('phone',$phone)->find();
                            if ($yingqi_count_exist) {
                                if ($yingqi_count_exist['course_id'] != $course_id) {
                                    db('crm_yingqi_count')->where('phone',$phone)->update(['course_id'=>$course_id]);
                                }
                            }
                            self::customerManageAdd($client_id);
                        }else{
                            db('goods_phone')->where(array('course_id'=>$course_id,'phone'=>$phone))->delete();
                            apiReturn(201,'批量操作失败，请重新提交');
                        }
                    }else{
                        db('goods_phone')->where(array('course_id'=>$course_id,'phone'=>$phone))->delete();
                        apiReturn(201,'营期下无子课程');
                    }
                }
            }elseif ($goods['search_type'] == 4 || $goods['search_type'] == 5 || $goods['search_type'] == 6) {
                $count = db('goods_phone')->alias('a')->join('tp_goods b','a.course_id=b.id')->where(['b.search_type'=>['in','4,5,6'],'a.phone'=>$phone])->count();
                if ($count == 1) {
                    $customer_datas['formali_yingqi'] = $course_id;
                    db('crm_customer')->where('client_id',$client_id)->update($customer_datas);
                }else{
                    db('goods_phone')->where(array('course_id'=>$course_id,'phone'=>$phone))->delete();
                    apiReturn(201,'已有系统课');
                }
            }elseif ($goods['search_type'] == 14 || $goods['search_type'] == 15 || $goods['search_type'] == 16) {
                $count = db('goods_phone')->alias('a')->join('tp_goods b','a.course_id=b.id')->where(['b.search_type'=>['in','14,15,16'],'a.phone'=>$phone])->count();
                if ($count == 1) {
                    $customer_datas['siwei_yingqi'] = $course_id;
                    db('crm_customer')->where('client_id',$client_id)->update($customer_datas);
                }else{
                    db('goods_phone')->where(array('course_id'=>$course_id,'phone'=>$phone))->delete();
                    apiReturn(201,'已有思维导图课');
                }
            }
        }
        return true;
    }

    // 清除课程缓存
    public function clearCustomerCache($phone)
    {
        if (empty($phone)) {
            apiReturn(201,'电话不能为空');
        }
        $login = db('user_course')->where('phone',$phone)->find();
        if (!empty($login)) {
            // apiReturn(201,'该用户无账号信息');
            $key = intval($login['id'].'1');
            try {
                Index::g_d_user_cache('delete_user_goods',$key);
                return true;
            } catch (Exception $e) {
                apiReturn(201,'清除失败'.$e->getMessage());
            }
        }
    }

    /**
     * 添加订单跟进记录
     * @param $order_id              订单id
     * @param $manage_id             跟进人id
     * @param $content               跟进内容
     * @param $pic                   跟进图片
     * @param $wait_deal             是否设置待办
     * @param $follow_manage_id      跟进人id 
     * @param $wait_deal_time        待办时间
     * @return bool
     */
    public function orderRecordAdd($order_id, $manage_id, $content, $pic = '')
    {
        if (empty($order_id) || empty($manage_id) || empty($content)) {
            apiReturn(201,'参数为空');
        }
        $oneManage = db('crm_manage')->field('id,username,name')->where('username',$manage_id)->find();
        if (empty($oneManage)) {
            apiReturn(201,'工号不存在');
        }
        $record_datas = array(
            'order_id'=>$order_id,
            'manage_id'=>$oneManage['username'],
            'manage_name'=>$oneManage['name'],
            'content'=>$content,
            'pic'=>$pic,
            'date'=>time()
        );
        db('crm_order_record')->insert($record_datas);
        return true;
    }

    // 添加客户归属
    public function customerManageAdd($client_id, $add = 0 ,$phone = 0)
    {
        if (!empty($phone)) {
            $customer = db('crm_customer')->where('phone',$phone)->find();
            $client_id = $customer['client_id'];
        }else{
            $customer = db('crm_customer')->where('client_id',$client_id)->find();
        }

        //邀约
        if (!empty($customer['yaoyue_manage_id'])) {
            $exist = db('crm_customer_manage')->where(array('client_id'=>$client_id,'manage_id'=>$customer['yaoyue_manage_id']))->find();
            $datas = array(
                'client_id'=>$customer['client_id'],
                'manage_id'=>$customer['yaoyue_manage_id'],
                'phone'=>$customer['phone'],
                'yaoyue_status'=>$customer['yaoyue_status'],
                'keqian_status1'=>$customer['keqian_status1'],
                'keqian_status2'=>$customer['keqian_status2'],
                'label'=>$customer['label'],
                'source'=>$customer['source'],
                'grade'=>$customer['grade'],
                'public_yingqi'=>$customer['public_yingqi'],
                'public_yingqi_course1'=>$customer['public_yingqi_course1'],
                'public_yingqi_course2'=>$customer['public_yingqi_course2'],
                'formali_yingqi'=>$customer['formali_yingqi'],
                'is_refund'=>$customer['is_refund'],
                'public_paytype'=>$customer['public_paytype'],
                'source_type'=>$customer['source_type'],
                'become_zhifu'=>$customer['become_zhifu'],
                'total'=>$customer['total'],
                'yaoyue_gonghai'=>$customer['yaoyue_gonghai']
            );
            if (empty($exist)) {
                if ($add == 1) {
                    $datas['date'] = time();
                    db('crm_customer_manage')->insert($datas);
                }
            }else{
                db('crm_customer_manage')->where('id',$exist['id'])->update($datas);
            }
        }

        //教务
        if (!empty($customer['jiaowu_manage_id'])) {
            $exist = db('crm_customer_manage')->where(array('client_id'=>$client_id,'manage_id'=>$customer['jiaowu_manage_id']))->find();
            $datas = array(
                'client_id'=>$customer['client_id'],
                'manage_id'=>$customer['jiaowu_manage_id'],
                'phone'=>$customer['phone'],
                'yaoyue_status'=>$customer['yaoyue_status'],
                'keqian_status1'=>$customer['keqian_status1'],
                'keqian_status2'=>$customer['keqian_status2'],
                'label'=>$customer['label'],
                'source'=>$customer['source'],
                'grade'=>$customer['grade'],
                'public_yingqi'=>$customer['public_yingqi'],
                'public_yingqi_course1'=>$customer['public_yingqi_course1'],
                'public_yingqi_course2'=>$customer['public_yingqi_course2'],
                'formali_yingqi'=>$customer['formali_yingqi'],
                'is_refund'=>$customer['is_refund'],
                'public_paytype'=>$customer['public_paytype'],
                'source_type'=>$customer['source_type'],
                'become_zhifu'=>$customer['become_zhifu'],
                'total'=>$customer['total'],
                'yaoyue_gonghai'=>$customer['yaoyue_gonghai']
            );
            if (empty($exist)) {
                if ($add == 1) {
                    $datas['date'] = time();
                    db('crm_customer_manage')->insert($datas);
                }
            }else{
                db('crm_customer_manage')->where('id',$exist['id'])->update($datas);
            }
        }
        return true;
    }

    // 积分添加
    public function customerIntegral($course_id, $phone ,$type, $integral)
    {
        if (empty($course_id) || empty($phone) || empty($type) || empty($integral)) {
            apiReturn(201,'参数错误');
        }
        $number = ['第一'=>1,'第二'=>2,'第三'=>3,'第四'=>4,'第五'=>5,'第六'=>6,'第七'=>7,'第八'=>8,'第九'=>9,'第十'=>10,'第十一'=>11,'第十二'=>12,'第十三'=>13,'第十四'=>14,'第十五'=>15,'第十六'=>16,'第十七'=>17,'第十八'=>18];
        $search_type = db('goods')->alias('a')->join('tp_goods_next b','a.id=b.goods_id')->where(['b.id'=>$course_id])->value('a.search_type');
        if ($search_type == 4 || $search_type == 5 || $search_type == 6) {
            $course_title = db('goods_next')->where('id',$course_id)->value('title');
            $course_title_jiang = explode('讲', $course_title)[0];
            $courseNumber = $number[$course_title_jiang];
            $couseExist = db('crm_customer_integral')->where(['phone'=>$phone,'number'=>$courseNumber,'type'=>$type])->find();
            if ($couseExist) {
                return false;
            }
            if (strpos($course_title,'开营须知')) {
                return false;
            }
            if (strpos($course_title,'家长会')) {
                return false;
            }
            $exist = db('crm_customer_integral')->where(['course_id'=>$course_id,'phone'=>$phone,'type'=>$type])->find();
            if (empty($exist)) {
                db('crm_customer_integral')->insert(['course_id'=>$course_id,'phone'=>$phone,'type'=>$type,'integral'=>$integral,'number'=>$courseNumber,'date'=>time()]);
                db('crm_customer')->where('phone',$phone)->setInc('integral',$integral);
            }
            return true;
        }else{
            return false;
        }
    }

    public function manageGroupInfo($manage_id)
    {
        $manage = db('manage')->where('id',$manage_id)->find();
        $manage['group_name'] = db('group')->where('id',$manage['groupid'])->value('name');
        return $manage;
    }

    // 设置列表
    public function getSettingList($type_name)
    {
        $settingList = db('setting')->alias('a')->join("tp_setting_type b","a.type=b.id")->field('a.id,a.name')->where(array('a.status'=>1,'b.name'=>$type_name))->select();
        return $settingList;
    }

    /**
     * 读取xls文件并返回数组
     * @param $file
     * @return array
     */
    public function readExcelFile($file)
    {
        vendor('PhpExcel.PHPExcel');
        $fileType=\PHPExcel_IOFactory::identify($file);
        $objReader=\PHPExcel_IOFactory::createReader($fileType);//创建phpexcel读取对象
        $sheetName='Sheet1';
        $objReader->setLoadSheetsOnly($sheetName);//加载指定sheet
        $objPHPExcel=$objReader->load($file);//加载指定sheet
        return $objPHPExcel->getActiveSheet()->toArray();
    }

    /**
     * 上传文件到阿里云对象存储
     * @param  string    $file    文件
     * @return string    $url     返回阿里云图片路径
     */
    public function uploadFile($file, $type=1)
    {
        $info = $file->validate(['size'=>20*1024*1024,'ext'=>'jpg,jpeg,png,mp4,mov,xlsx,xls,txt'])->move(ROOT_PATH . 'public' . DS . 'uploads');
        if ($info) {
            $extInfo = $info->getInfo();
            // var_dump($extInfo);exit;
            // $ext_array = explode('/', $extInfo['type']);
            $ext_array = '';
            if(strpos($extInfo['type'],'image') !== false){
                $ext_array = 'jpg';
            }
            if(strpos($extInfo['name'],'jpg') !== false){
                $ext_array = 'jpg';
            }
            if(strpos($extInfo['name'],'jpeg') !== false){
                $ext_array = 'jpeg';
            }
            if(strpos($extInfo['name'],'png') !== false){
                $ext_array = 'png';
            }
            if(strpos($extInfo['type'],'excel') !== false){
                $ext_array = 'xlsx';
            }
            if(strpos($extInfo['name'],'xlsx') !== false){
                $ext_array = 'xlsx';
            }
            if(strpos($extInfo['name'],'xls') !== false){
                $ext_array = 'xlsx';
            }
            $filePath = $url = '/uploads/'.str_replace('\\', '/', $info->getSaveName());
            if ($type == 1) {
                $url = aliyunoss::getOssUrl('.'.$filePath, $ext_array);
            }
            return $url;
        }else{
            apiReturn(201,'上传失败，请重新上传');
        }
    }

    /**
     * 缩略图
     * @param  string    $url     图片路径
     * @param  string    $width   图片宽度
     * @param  string    $height  图片高度
     * @return string    $thumb   返回缩略图路径
     */
    public function thumbPic($url, $width, $height)
    {
        $urlInfo = time().'.png';
        $absolute_path = "./uploads/ossimg/".$urlInfo;
        download($url, $absolute_path);

        $image = Image::open($absolute_path);
        $url_array = explode('.', $absolute_path);
        $thumb = $url_array[1].'_thumb.'.$url_array[2];
        $image->thumb($width, $height ,3)->save('.'.$thumb);

        $url = aliyunoss::getOssUrl('.'.$thumb, $url_array[2]);
        return $url;
    }

    // 获取工号分组列表
    public function getFenzuList($zu_id = null)
    {
        $whereid = '';
        if (empty($zu_id)) {
            $manage_author = self::getManageAuthor();

            if ($manage_author['is_xiaoshou'] == 1) {
                if ($manage_author['pid'] == 0) {
                    $next_zu_list = db('crm_fenzu')->where(array('pid'=>$manage_author['zu_id']))->column('id');
                    $zu_ids = [];
                    $zu_ids[] = $manage_author['zu_id'];
                    foreach ($next_zu_list as $key => $value) {
                        $zu_ids[] = $value;
                    }
                    $whereid = array('in',implode(',', $zu_ids));
                }else{
                    $whereid = $manage_author['zu_id'];
                }
            }
        }else{
            $zu_info = db('crm_fenzu')->where('id',$zu_id)->find();
            if (empty($zu_info)) {
                apiReturn(201,'分组信息不存在');
            }
            if ($zu_info['pid'] == 0) {
                $next_zu_list = db('crm_fenzu')->where(array('pid'=>$zu_info['id']))->column('id');
                $zu_ids = [];
                $zu_ids[] = $zu_info['id'];
                foreach ($next_zu_list as $key2 => $value2) {
                    $zu_ids[] = $value2;
                }
                $whereid = array('in',implode(',', $zu_ids));
            }else{
                $whereid = $zu_info['id'];
            }
        }
        
        return $whereid;
    }

    // 获取工号分组列表
    public function getManageList($zu_id = null)
    {
        $whereusername = [];
        if (empty($zu_id)) {
            $manage_author = self::getManageAuthor();
            
            if ($manage_author['is_xiaoshou'] == 1) {
                if ($manage_author['pid'] == 0) {
                    $wherezuid = self::getFenzuList();
                    if (!empty($wherezuid)) {
                        $where1['zu_id'] = $wherezuid;
                        $zuManageList = db('crm_manage')->where($where1)->column('username');
                        $whereusername = array('in',implode(',', $zuManageList));
                    }
                }else{
                    if (strpos($manage_author['group_name'], '主管')) {
                        $zuManageList = db('crm_manage')->where('zu_id',$manage_author['zu_id'])->column('username');
                        $whereusername =  array('in',implode(',', $zuManageList));
                    }else{
                        $whereusername = $manage_author['username'];
                    }
                }
            }
        }else{
            $where2['zu_id'] = $zu_id;
            $zuManageList = db('crm_manage')->where($where2)->column('username');
            $whereusername =  array('in',implode(',', $zuManageList));
        }

        return $whereusername;
    }

    public function getManageAuthor()
    {
        $manage_author['username'] = $this->username;
        $manage_author['manage_type'] = $this->manage_type;
        $manage_author['banzhuren_type'] = $this->banzhuren_type;
        $manage_author['zu_id'] = $this->zu_id;
        $manage_author['pid'] = $this->pid;
        $manage_author['gonghai'] = $this->gonghai;
        $manage_author['is_xiaoshou'] = $this->is_xiaoshou;
        $manage_author['group_name'] = $this->group_name;
        return $manage_author;
    }

    /**
     * @param $phone  客户电话
     * @param $type   类型（1下单 2扫码 3合并）
     * @param $phone2 被合并的客户电话
     **/
    public function yingqiCount($phone, $type, $phone2 = '')
    {
        $customer = db('crm_customer')->field('name,grade,manage_id,public_yingqi,date')->where('phone',$phone)->find();
        if (empty($customer)) {
            return 201;
        }

        $exist = db('crm_yingqi_count')->where('phone',$phone)->find();
        if (!$exist) {
            if ($type == 2) {
                if (empty($customer['public_yingqi'])) {
                    return 202;
                }
                $sn = db('crm_scan')->where('phone',$phone)->value('order_sn');
                $datas = [
                    'sn'=>$sn,
                    'name'=>$customer['name'],
                    'grade'=>$customer['grade'],
                    'phone'=>$phone,
                    'phone2'=>$phone2,
                    'course_id'=>$customer['public_yingqi'],
                    'manage_id'=>$customer['manage_id'],
                    'type'=>$type,
                    'date'=>time()
                ];
                db('crm_yingqi_count')->insert($datas);
                return true;
            }else{
                $cooperation = db('cooperation')->field('order_sn,realname,date')->where(['number'=>2980,'phone'=>$phone2])->find();
                if (!empty($cooperation)) {
                    $datas = [
                        'sn'=>$cooperation['order_sn'],
                        'name'=>$cooperation['realname'],
                        'grade'=>$customer['grade'],
                        'phone'=>$phone,
                        'phone2'=>$phone2,
                        'course_id'=>$customer['public_yingqi'],
                        'manage_id'=>$customer['manage_id'],
                        'type'=>$type,
                        'date'=>$cooperation['date']
                    ];
                    db('crm_yingqi_count')->insert($datas);
                    return true;
                }else{
                    // apiReturn(201,'未查询到合并客户支付订单');
                    return false;
                }
            }
        }else{
            // apiReturn(201,'已存在订单');
            return 203;
        }
    }

    // 删除大屏数据
    public function yingqiCountDelete($phone)
    {
        $exist = db('crm_yingqi_count')->where('phone',$phone)->find();
        if ($exist) {
            db('crm_yingqi_count')->where('phone',$phone)->delete();
            return true;
        }else{
            return false;
        }
    }
    
}
?>